﻿using CQRSLearn.Domain;
using CQRSLearn.Persistence;
using MediatR;

namespace CQRSLearn.Features.Products.Commands.Update
{
    public class UpdateProductCommandHandler : IRequestHandler<UpdateProductCommand,int>
    {
        private readonly AppDbContext context;

        public UpdateProductCommandHandler(AppDbContext context)
        {
            this.context = context;
        }

        public async Task<int> Handle(UpdateProductCommand request, CancellationToken cancellationToken)
        {
            var product = await context.Products.FindAsync(request.Id);
            if (product == null)
            {
                return 0;
            }
            product.Name = request.Name;
            product.Description = request.Description;
            product.Price= request.Price;
            return await context.SaveChangesAsync();
        }
    }
}
